using UnityEngine;
using System.Collections;

public class Grid : MonoBehaviour
{
    public int Width;
    public int HeightPerPlayer;
    public int SpaceFromCenter;

    private static Grid instance;
    private GameObject[,,] grid;
    private bool[,] dirtyLines;

    // Use this for initialization
    void Start()
    {
        instance = this;
        grid = new GameObject[2, Width, HeightPerPlayer];
    }

    void Update()
    {
        //Move all cubes to their correct position
        for (int h = 0; h < HeightPerPlayer; h++)
        {
            for (int w = 0; w < Width; w++)
            {
                GameObject cube = grid[0, h, w];
                if (cube != null)
                {
                    Vector3 newPosition = new Vector3(-SpaceFromCenter - HeightPerPlayer + h, -1, Width / 2f + w);
                    cube.transform.position = Vector3.Lerp(cube.transform.position, newPosition, 5 * Time.smoothDeltaTime);
                }
                cube = grid[1, h, w];
                if (cube != null)
                {
                    Vector3 newPosition = new Vector3(SpaceFromCenter + HeightPerPlayer - h, -1, Width / 2f + w);
                    cube.transform.position = Vector3.Lerp(cube.transform.position, newPosition, 5 * Time.smoothDeltaTime);
                }
            }
        }
    }

    public static bool CheckBlockCollision(BlockController block)
    {
        return false;
    }

    public static void SetBlock(BlockController block)
    {
        instance.checkForCompletedLines(0);
    }

    void checkForCompletedLines(int playerField)
    {
        for (int h = 0; h < HeightPerPlayer; h++)
        {
            //Check only for dirty lines
            if (dirtyLines[playerField, h])
            {
                bool clear = true;
                for (int w = 0; w < Width; w++)
                {
                    if (grid[playerField, h, w] == null)
                    {
                        clear = false;
                        break;
                    }
                }

                //If line is full, destroy it
                if (clear)
                    deleteLine(playerField, h);

                //Line is not dirty anymore
                dirtyLines[playerField, h] = false;
            }
        }
    }

    void deleteLine(int playerField, int height)
    {
        //Destroy all cubes in the line
        for (int w = 0; w < Width; w++)
            Destroy(grid[playerField, height, w]);
        
        //Move all lines down
        for (int h = height + 1; h < HeightPerPlayer; h++)
        {
            dirtyLines[playerField, h - 1] = dirtyLines[playerField, h];
            for (int w = 0; w < Width; w++)
                grid[playerField, h - 1, w] = grid[playerField, h, w];
        }

        //Clear the last line
        for (int w = 0; w < Width; w++)
            grid[playerField, HeightPerPlayer - 1, w] = null;
    }
}
